package com.enjoyor.soa.traffic.restt.tpcms.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.enjoyor.soa.traffic.api.dto.tpcms.InfoWhiteListDto;
import com.enjoyor.soa.traffic.frame.annotation.Auth;
import com.enjoyor.soa.traffic.frame.security.bean.dto.UserDto;
import com.enjoyor.soa.traffic.restt.tpcms.dubbo.IWhiteListConsumeService;
import com.enjoyor.soa.traffic.util.enums.EnumAppCode;
import com.enjoyor.soa.traffic.util.helper.ResultHelper;
import com.enjoyor.soa.traffic.util.pojo.Page;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Auth
@RestController
@RequestMapping({ "/whiteList" })
@Api("WhiteListController页面请求")
@CrossOrigin
public class WhiteListController {

    @Autowired
    private IWhiteListConsumeService whiteListConsumeService;

    @GetMapping(value = { "/getWhiteListList" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation(value = "获取白名单车辆列表数据", notes = "查询数据库中符合条件的操作记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "whiteListId", value = "whiteListId", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "licenseNumber", value = "licenseNumber", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "licenseTypeId", value = "licenseTypeId", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "vehicleTypeId", value = "vehicleTypeId", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "funTypeId", value = "funTypeId", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "vehicleDept", value = "vehicleDept", paramType = "query", dataType = "string") })
    public ResultPojo getWhiteListList(@RequestParam(defaultValue = "", value = "whiteListId") String whiteListId,
            @RequestParam(defaultValue = "", value = "licenseNumber") String licenseNumber,
            @RequestParam(defaultValue = "", value = "licenseTypeId") String licenseTypeId,
            @RequestParam(defaultValue = "", value = "vehicleTypeId") String vehicleTypeId,
            @RequestParam(defaultValue = "", value = "funTypeId") String funTypeId,
            @RequestParam(defaultValue = "", value = "vehicleDept") String vehicleDept, HttpServletRequest request) {
        /*String result = ResultHelper.getResultJson(whiteListConsumeService.getWhiteListList(whiteListId, licenseNumber,
                licenseTypeId, vehicleTypeId, funTypeId, vehicleDept)).toJSONString();*/
        return whiteListConsumeService.getWhiteListList(whiteListId, licenseNumber,licenseTypeId, vehicleTypeId, funTypeId, vehicleDept);
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    @GetMapping(value = { "/getWhiteListPage" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "whiteListId", value = "whiteListId", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "licenseNumber", value = "licenseNumber", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "licenseTypeId", value = "licenseTypeId", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "vehicleTypeId", value = "vehicleTypeId", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "funTypeId", value = "funTypeId", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "vehicleDept", value = "vehicleDept", paramType = "query", dataType = "string") })
    public ResultPojo getWhiteListPage(@RequestParam(defaultValue = "", value = "whiteListId") String whiteListId,
            @RequestParam(defaultValue = "", value = "licenseNumber") String licenseNumber,
            @RequestParam(defaultValue = "", value = "licenseTypeId") String licenseTypeId,
            @RequestParam(defaultValue = "", value = "vehicleTypeId") String vehicleTypeId,
            @RequestParam(defaultValue = "", value = "funTypeId") String funTypeId,
            @RequestParam(defaultValue = "", value = "vehicleDept") String vehicleDept, Page page,
            HttpServletRequest request) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("whiteListId", whiteListId);
        map.put("licenseNumber", licenseNumber);
        map.put("licenseTypeId", licenseTypeId);
        map.put("vehicleTypeId", vehicleTypeId);
        map.put("funTypeId", funTypeId);
        map.put("vehicleDept", vehicleDept);
        page.setMapBean(map);
        //String result = ResultHelper.getResultJson(whiteListConsumeService.getWhiteListPage(page)).toJSONString();
        return whiteListConsumeService.getWhiteListPage(page);
    }

    @PostMapping(value = { "/insertWhiteList" }, consumes = { "application/json; charset=UTF-8" }, produces = {
            "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("新增白名单车辆")
    public ResultPojo insertWhiteList(@RequestBody InfoWhiteListDto infoWhiteList, HttpServletRequest request) {
        int checkNum = (int) whiteListConsumeService
                .insertWhiteList(infoWhiteList, ((UserDto) request.getAttribute("currentUser")).getPersonName())
                .getResultList();
        if (checkNum == 0) {
            return ResultHelper.getResult("success");
        } else {
            return ResultHelper.getResult(null, EnumAppCode.DEVIALTE_UNIQUE, "车牌重复", "车牌重复");
        }

    }

    @PutMapping(value = { "/editWhiteList" }, consumes = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("更新白名单数据")
    public ResultPojo editWhiteList(@RequestBody InfoWhiteListDto infoWhiteList, HttpServletRequest request) {
        return whiteListConsumeService.editWhiteList(infoWhiteList,
                ((UserDto) request.getAttribute("currentUser")).getPersonName());
    }

    @DeleteMapping(value = { "/deleteWhiteList" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("删除数据")
    @ApiImplicitParam(name = "whiteListId", value = "whiteListId", paramType = "query", defaultValue = "W2018050314411012863389", required = true, dataType = "string")
    public ResultPojo deleteWhiteList(@RequestParam("whiteListId") String whiteListId, HttpServletRequest request) {
        whiteListConsumeService.deleteWhiteList(whiteListId,
                ((UserDto) request.getAttribute("currentUser")).getPersonName());
        ResultPojo result = new ResultPojo();
        result = ResultHelper.getResult("success");
        return result;
    }

}
